/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------

**************************************************************************** */

	/***************************************************
	 * Nhelper : php단에서 nlog헬퍼를 사용 할 경우 화면에 출력
	 ***************************************************/
	var Nhelper = {
		call : function(){
			$('.nlog').each(function(){
				$(this).children('.nlogClose').click(function(){
					$(this).parent().remove();
				});
			});
		}
	};


	/***************************************************
	 * Nlog : 디버그 콘솔에 메시지 출력
	 ***************************************************/
	/*
	*	example)
	*		Nlog.put(var);
	*		Nlog.puts("foo","var");
	*		Nlog.puts(Nlog.var_dump(obj));
	*/
	var Nlog = {
		put : function(param1){
			if(NlogEnableMode=='production'){
				return;
			}
			try{
				console.log(param1);
			} catch(e) {
				return;
			}
		},
		puts : function(param1, param2){
			if(NlogEnableMode=='production'){
				return;
			}
			var locationURL = this.host();

			if(Browser.ie && Browser.ie6 && Browser.ie7) {
				if(param2 == '' || param2 == undefined){
					try{
						alert(''+locationURL+'>>'+param1+'');
					} catch(e) {
						return;
					}
				} else {
					try{
						alert(''+param1+'>>'+param2);
					} catch(e) {
						return;
					}
				}
			} else {
				if(param2 == '' || param2 == undefined){
					try{
						console.log(''+locationURL+'>>'+param1+'');
					} catch(e) {
						return;
					}
				} else {
					try{
						console.log(''+param1+'>>'+param2);
					} catch(e) {
						return;
					}
				}
			}
		},
		cls  : function(){
			try{ console.clear(); }catch(e){}
		},
		var_dump : function(arr, level){
			if(NlogEnableMode=='production'){
				return;
			}
			var var_dumped_text = "";
				if(!level) level = 0;

				//The padding given at the beginning of the line.
				var level_padding = "";
				for(var j=0;j<level+1;j++) level_padding += ">>";

				if(typeof(arr) == 'object') { //Array/Hashes/Objects
					for(var item in arr) {
						var value = arr[item];

						if(typeof(value) == 'object') { //If it is an array,
							var_dumped_text += level_padding + "'" + item + "' ...\n";
							var_dumped_text += Nlog.var_dump(value,level+1);
						} else {
							var_dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
						}
					}
				} else { //Stings/Chars/Numbers etc.
					var_dumped_text = ">>"+arr+"<<("+typeof(arr)+")";
				}
				return var_dumped_text;
		},
		host : function(){
			var Dns;
			Dns=location.href;
			Dns=Dns.split("//");
			Dns="http://"+Dns[1].substr(0,Dns[1].indexOf("/"));
			return Dns;
		}
	};


	/***************************************************
	 * Najax : AJAX CALL
	 ***************************************************/
	var Najax = {
		form : null,
		mode : '',
		nameValues : '',
		afterCallback : function(data){},
		init : function(oArg){
			this.mode = oArg.mode;
			Nlog.put('control.js Najax called>>this.mode>>'+this.mode);
			this.form = $(oArg.oForm);
			this.nameValues = oArg.nameValues;
			this.afterCallback = oArg.afterCallback;
		},
		call : function(){
			var url = this.form.attr('action') + '?mode='+this.mode;
			postValue['csrf_test_name'] = $(this.form).find('input[name=csrf_test_name]').val();
			Nlog.put('control.js postValue >>');
			console.dir(postValue);

			for(var i = 0; i < this.nameValues.length; i++){
				if($(this.form).find('input[name="'+this.nameValues[i]+'"]').attr('type') == 'checkbox'){
					if($(this.form).find('input[name="'+this.nameValues[i]+'"]').prop("checked") == false){
						postValue[this.nameValues[i]] = $.trim(GAes.enc('n', GAesKey)); // enum (y|n)
					} else {
						postValue[this.nameValues[i]] = $.trim(GAes.enc($(this.form).find('input[name="'+this.nameValues[i]+'"]:checked').val(), GAesKey));
					}
				} else if($(this.form).find('input[name="'+this.nameValues[i]+'"]').attr('type') == 'radio'){
					if($(this.form).find('input[name="'+this.nameValues[i]+'"]:checked').val() == undefined){
						postValue[this.nameValues[i]] = $.trim(GAes.enc('', GAesKey));
					} else {
						postValue[this.nameValues[i]] = $.trim(GAes.enc($(this.form).find('input[name="'+this.nameValues[i]+'"]:checked').val(), GAesKey));
					}
				} else if($(this.form).find('select[name="'+this.nameValues[i]+'"]').length > 0){
					if($(this.form).find('select[name="'+this.nameValues[i]+'"] option:selected').val() == undefined){
						postValue[this.nameValues[i]] = $.trim(GAes.enc('', GAesKey));
					} else {
						postValue[this.nameValues[i]] = $.trim(GAes.enc($(this.form).find('select[name="'+this.nameValues[i]+'"] option:selected').val(), GAesKey));
						//console.log( $(this.form).find('select[name="'+this.nameValues[i]+'"] option:selected').val() );
					}
				} else if($(this.form).find('textarea[name="'+this.nameValues[i]+'"]').length > 0){
					if($(this.form).find('textarea[name="'+this.nameValues[i]+'"]').val() == undefined ||
						$(this.form).find('textarea[name="'+this.nameValues[i]+'"]').val() == '') {
						postValue[this.nameValues[i]] = $.trim(GAes.enc('', GAesKey));
					} else {
						postValue[this.nameValues[i]] = $.trim(GAes.enc($(this.form).find('textarea[name="'+this.nameValues[i]+'"]').val(), GAesKey));
					}
				} else if($(this.form).find('input[name="'+this.nameValues[i]+'"]').attr('type') == 'hidden') {
					postValue[this.nameValues[i]] = $(this.form).find('input[name="'+this.nameValues[i]+'"]').val();
				} else {
					if($(this.form).find('input[name="'+this.nameValues[i]+'"]').val() == undefined ||
						$(this.form).find('input[name="'+this.nameValues[i]+'"]').val() == '') {
						postValue[this.nameValues[i]] = $.trim(GAes.enc('', GAesKey));
					} else {
						postValue[this.nameValues[i]] = $.trim(GAes.enc($(this.form).find('input[name="'+this.nameValues[i]+'"]').val(), GAesKey));
					}
				}
			}
			Nlog.put('control.js postValue >>');
			Nlog.put(postValue);

			var posting = $.post(url, postValue);
			posting.done(function(data) {
				console.log('control.js>>' + data);
				try{
					var jsonObject = jQuery.parseJSON(data);
				} catch(e) { console.error('control.js>>parseJSON fail'); }
				try{
					Najax.afterCallback(jsonObject);
				} catch(e) { console.error('control.js>>acfterCallback Call fail'); }
			});

		},
		init02:function(oArg){
			this.form = $(oArg.oForm);
			this.afterCallback = oArg.afterCallback;
		},
		r01:function(queryString){
			postValueR01['csrf_test_name'] = $(this.form).find('input[name=csrf_test_name]').val();
			var posting = $.post(queryString, postValueR01);
			posting.done(function(data) {
				try{
					var jsonObject = jQuery.parseJSON(data);
				} catch(e) { console.error('control.js>>parseJSON fail'); }
				try{
					Najax.afterCallback(jsonObject);
				} catch(e) { console.error('control.js>>acfterCallback Call fail'); }
			});
		},
		r02 : function(){
			var url = this.form.attr('action') + '?mode='+this.mode;
			var postValue = {
				csrf_test_name: $(this.form).find('input[name=csrf_test_name]').val(),
				GAesKey : GAesKey
			};
		}
	};


	/***************************************************
	 * LayoutResize : 레이아웃을 리사이징
	 ***************************************************/
	var LayoutResize = {
		call : function(){
			var wh = $(window).height();
			var cth = $('.navbar.navbar-default').height()+1;
			$('body').css('min-height', wh-cth);
			$('.content').css('min-height', wh-cth);
		}
	};


	/***************************************************
	 * jsonToHtml : json을 Html table tr>td로 변환
	 ***************************************************/
	var jsonToHtml = {
		getListData : function(data){
			var dataln = '';
			console.log(data.listdata.length);
			$.each(data.listdata, function(key, val) {
				dataln += '<tr';
				dataln += ' onclick="'+val.clickEvent+'"';
				dataln += '>';
				$.each(val.childes, function(k, v){
					dataln += '<td>'+v.textValue+'</td>';
				});
				dataln += '</tr>';
			});
			if(data.listdata.length == 0){
				var colspan = $('.uIdTable01 thead th').length;
				dataln = '<td colspan="'+colspan+'" class="tac padt10 padb10">조회된 결과가 없습니다.</td>';
			}
			return dataln;
		}
	};


	/***************************************************
	 * call
	 ***************************************************/
    $(function(){
        LayoutResize.call();
        $.cookie('name', 'value');
        var c = $.cookie('n_isOffice');
        if(c){
            $('.navbar-default').add('.sidebar-nav').add('footer').add('h2').add('.header').add('h3').hide();
            $('body').css('background-color','white');
            $('.content').css('border-right-width','0px');
            $('title').text('test');
        }
    });
    $(window).resize(function(){
        LayoutResize.call();
    });

    $(function(){
        /* 취소 버튼 바인딩 */
        $('.uIdBtnCancel01').add('.uIdBtnCancel02').add('.uIdBtnCancel03').click(function(){
            history.go(-1);
        });

        /* 검색시 enterKey 무효화 */
        $('.nSelectVal').keypress(function (e) {
            if(e.which == 13) {
                $('.uIdSearchBtn01').click();
                return false;
            }
        });
    });

	$(function(){
		Nhelper.call();
	});

    /* Ajax Indicate */
    $(function(){
        $('body').append('<div id="ajaxBusy"><p><img src="/static/img/ajax-loader.gif"></p></div>');
        $('#ajaxBusy').css({
            display:"none",
            margin:"0px",
            paddingLeft:"-16px",
            paddingRight:"0px",
            paddingTop:"-16px",
            paddingBottom:"0px",
            position:"absolute",
            right:"50%",
            top:"50%",
            "z-index":"9999999999999999999999",
            width:"auto"
        });
    });
    // Ajax activity indicator bound to ajax start/stop document events
    $(document).ajaxStart(function(){
        $('#ajaxBusy').show();
    }).ajaxStop(function(){
        $('#ajaxBusy').hide();
    });